package com.shop.loan.proxy;

import com.alibaba.fastjson.JSON;
import com.shop.loan.model.view.MobileCodeShow;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.util.Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class MobileCodeShowProxy implements DataProxy<MobileCodeShow> {

    @Resource
    RedisUtil redisUtil;

    @Resource
    Util util;

    @Value("${sys.adminPhone}")
    private String adminPhone;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.removeIf(condition -> "mobile".trim().equals(condition.getKey()));
        return null;
    }


    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        String mobile = util.getSearchParam("mobile", 1);
        if (!StringUtils.hasLength(mobile)) {
            return;
        }
        if(adminPhone.trim().equals(mobile)){
            //管理员密码无法显示
            return;
        }
        MobileCodeShow mobileCodeShowVo = new MobileCodeShow();
        mobileCodeShowVo.setMobile(mobile);
        mobileCodeShowVo.setCode(redisUtil.get(RedisCache.phoneCode + mobile));
        Map map = JSON.parseObject(JSON.toJSONString(mobileCodeShowVo), Map.class);
        list.add(map);
    }
}
